package com.yeqing.iodemo;

import java.io.*;

public class BufferDemo {


    public static void 使用基本的流读取数据() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("/Users/cyq/Downloads/WeChatMac.dmg");
        FileOutputStream fos = new FileOutputStream("/Users/cyq/Downloads/WeChatMacCopy.dmg");

        int data;
        while ((data = fis.read()) != -1) {
            fos.write(data);
        }
        System.out.println("使用基本的流读取数据耗时：" + (System.currentTimeMillis() - start));
        fis.close();
        fos.close();
    }

    public static void 使用缓冲流的读取数据() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/cyq/Downloads/WeChatMac.dmg"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/Users/cyq/Downloads/WeChatMacCopy.dmg"));

        int data;
        while ((data = bufferedInputStream.read()) != -1) {
            bufferedOutputStream.write(data);
        }

        System.out.println("使用缓冲流的读取数据耗时：" + (System.currentTimeMillis() - start));
    }

    public static void 使用缓冲流的读取数据一次读取8M个的字节数组() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/cyq/Downloads/WeChatMac.dmg"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/Users/cyq/Downloads/WeChatMacCopy.dmg"));

        int len;
        byte[] data = new byte[1024 * 1024 * 8];
        while ((len = bufferedInputStream.read(data)) != -1) {
            bufferedOutputStream.write(data, 0, len);
        }

        System.out.println("使用缓冲流的读取数据耗时：" + (System.currentTimeMillis() - start));
    }

    public static void 字符缓冲流读取内容到另一个文件() throws Exception {
        try(
                BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/cyq/Develop/三国/张飞2.txt"));
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/Users/cyq/Develop/三国/张飞22.txt"));
        ) {
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
        }
    }

    public static void 字符缓冲流的正确姿势() {
        try(
                FileInputStream fis = new FileInputStream("/Users/cyq/Downloads/WeChatMac.dmg");
                BufferedInputStream bis = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream("/Users/cyq/Downloads/WeChatMacCopy22.dmg");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                ) {
            int len;
            byte[] data = new byte[1024 * 1024 * 8];
            while ((len = bis.read(data)) != -1) {
                bos.write(data, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        使用基本的流读取数据();

//        使用缓冲流的读取数据();

//        使用缓冲流的读取数据一次读取8M个的字节数组();

//        字符缓冲流读取内容到另一个文件();

        字符缓冲流的正确姿势();
    }
}
